﻿@{
    ViewBag.Title = "RequireGoods";
}

@section scripts
{   
    <script src="@Url.Content("~/Scripts/shared/share.js?v=")@ViewBag.version" type="text/javascript"></script> 
    <script src="@Url.Content("~/Scripts/shared/shown.js?v=")@ViewBag.version" type="text/javascript"></script>  
    <script src="@Url.Content("~/Scripts/Bill/Bill.js?v=")@ViewBag.version" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/Bill/scan.js?v=")@ViewBag.version" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/common/Print.js?v=")@ViewBag.version" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/zxx.drag.1.0.js?v=")@ViewBag.version" type="text/javascript"></script>
    <link href="~/Scripts/miniui/themes/default/medium-mode.css" rel="stylesheet" type="text/css" />      
    <script src="@Url.Content("~/Scripts/common/export.js?v=")@ViewBag.version" type="text/javascript"></script> 
    <style type="text/css">
        .window-mask {
                position: absolute;
                left: 0;
                top: 0;
                width: 100%;
                height: 100%;
                filter: alpha(opacity=40);
                opacity: 0.40;
                font-size: 1px;
                overflow: hidden;
                display:none;
                background-color: gray;
            }
    </style>
}
   <form id="form1" runat="server" method="post" style="height: 100%">
    
<div class="mini-toolbar" style="padding:2px;border-top:0;border-left:0;border-right:0;"> 
    <table width = "1000px;">
        <tr>
            <td width = "1000px;">
                <a class="mini-button" id="new" name="new" iconCls="icon-new" onclick="NewBill" plain="true" visible="@ViewBag.save">新增</a>
                <a class="mini-button" id="save" name="save" iconCls="icon-save" onclick="saveBill" plain="true" visible="@ViewBag.save">保存</a>
                <a class="mini-button" id="addrow" name="addrow" iconCls="icon-addnew" onclick="addRow" plain="true" visible="@ViewBag.save">增行</a>
                <a class="mini-button" id="delrow" name="delrow" iconCls="icon-remove" onclick="removeRow" plain="true" visible="@ViewBag.save">删行</a>
                <a class="mini-button" id="modify" name="modify" iconCls="icon-edit" onclick="modify" plain="true" visible="@ViewBag.save">改单</a>
                <a class="mini-button" id="check" name="check" iconCls="icon-ok" onclick="checkBill" plain="true" visible="@ViewBag.check">审核</a>
                <a class="mini-menubutton" plain="true" iconCls="icon-expand" menu="#popupMenu" visible="@ViewBag.save">选项</a>
                <a class="mini-button" id="print" iconCls="icon-print" onclick="printBillMyseft" plain="true" visible="@ViewBag.print">打印</a>
                <a class="mini-button" id="getdata" iconCls="icon-collapse" onclick="getBillData" plain="true">浏览</a>
                <span class="separator"></span>
                <a class="mini-button" id="close" iconCls="icon-close" onclick="CloseWindowBill" plain="true">关闭</a>
            </td>
        </tr>
    </table>                 
</div>
    
    <div id = "DivHeader">
        <table style="width:770px;">
            <tr>
                <td width ="80px" align="right">要货门店：</td>
                <td width ="190px">    
                     @Html.Partial("~/Views/Modular/LookupStockInfo.cshtml") 
                </td>
                <td width ="80px" align="right">开单日期：</td>
                <td width ="190px">    
                    <input name="FDate" class="mini-datepicker" style="width:190px;" />
                </td>
                 <td width ="80px" align="right">单据编号：</td>
                 <td width ="150px">    
                    <input name="FBillNo" class="mini-textbox" required="true" readonly="readonly"style="width:150px;"/>
                 </td>
            </tr>
            <tr>
                <td align="right">配送门店：</td>
                <td >
                  @Html.Partial("~/Views/Delivery/LookupStockRelevant.cshtml")      
               </td>
                <td align="right">单据备注：</td> 
                <td >  
                    <input name="FRemark" class="mini-textbox"  style="width:190px;"/>  
                </td>
                <td align="right">单据状态：</td>
                <td >    
                    <input name="txtCheckFlag" class="mini-textbox" readonly="readonly"style="width:150px;"/></td>
            </tr>
        </table>
    </div>
    <div class="mini-fit">
        @Html.Partial("~/Views/Modular/Bill/DataGridPra.cshtml")
    </div>

     @Html.Partial("~/Views/Modular/Bill/Footer.cshtml")
       
    </form>
        @Html.Partial("~/Views/Modular/Bill/ScanWin.cshtml")

    <input id="type" name="type" value="@ViewBag.type" type="hidden"/>
    <input id="billid" name="type" value="@ViewBag.billid" type="hidden"/>
    <input id="delivery" name="delivery" value="@ViewBag.delivery" type="hidden"/>

    <!--menu-->
    <ul id="popupMenu" class="mini-menu" style="display:none;">
        <li id ="import" name="import" iconCls="icon-redo" onclick="ExcelImport">从Excel文件导入</li>     
        <li id ="export" name="export" iconCls="icon-node" onclick="ExcelExportBill">导出明细</li>       
	    <li id ="ScanAdd" name="ScanAdd" iconCls="icon-find" onclick="ScanAdd">扫描录入</li>        
    </ul>

        <script type="text/javascript">
            mini.parse();
            var form = new mini.Form("form1");
            var grid = mini.get("datagrid1");
            var uID = parent.window.getSystemAppID();
            var uCode = parent.window.getSystemAppCode();
            var uName = parent.window.getSystemAppName();

            $(function () {
                var oBox = document.getElementById("scan");
                var oBar = document.getElementById("title");
                if (id == 0) BillState(0);
                InitScan();
                startDrag(oBar, oBox);
            });

            var id = $("#billid").val();
            if (id != null) {
                mini.getbyName("KInterID").setValue(id);
            }
            //var datatype = $("#type").val();
            var datatype = getQueryString("datatype");

            LoadData();

            function LoadData() {
                var minterid = mini.getbyName("KInterID").value;
                $.ajax({
                    url: '../Bill/GetBillData',
                    data: { mSystemAppID: uID, mBillID: minterid, mDataType: datatype, mDelivery: $("#delivery").val() },
                    dataType: 'text',
                    async: false,
                    success: function (text) {
                        var dataR = mini.decode(text);
                        grid.setData(dataR.datadetail);
                        if (dataR.datamaster.length > 0) {
                            var mdata = dataR.datamaster[0];
                            form.setData(mdata);
                            mini.getbyName("FStockID").setValue(mdata.FStockID);
                            mini.getbyName("FStockID").setText(mdata.FStockNumber);                            
                            mini.getbyName("FRStockID").setValue(mdata.FRStockID);
                            mini.getbyName("FRStockID").setText(mdata.FRStockNumber);
                            if (mdata.FCheckFlag == 1) {
                                BillState(2);
                                mini.getbyName("ScanAdd").setEnabled(false);
                                mini.get("import").setEnabled(false);
                            }
                            else {
                                BillState(1);
                                mini.getbyName("ScanAdd").setEnabled(false);
                                mini.get("import").setEnabled(false);
                            }
                            if ((mdata.KInterID == "0" || mdata.KInterID == null) && dataR.datadetail.length == 0) {
                                newBills();
                                mini.getbyName("ScanAdd").setEnabled(true);
                                mini.get("import").setEnabled(true);
                            }
                        }
                        else {
                            newBills();
                            mini.getbyName("ScanAdd").setEnabled(true);
                            mini.get("import").setEnabled(true);
                        }
                    }
                });
            }

            function newBills() {
                NewBill();
                var date = new Date();
                mini.getbyName("FBillDate").setValue(date);
                mini.get("ScanAdd").setEnabled(true);
                mini.get("import").setEnabled(true);
            }

            function checkValid() {
                if ((mini.getbyName("FBillerID").getValue() == "") || (mini.getbyName("FBillerID").getValue() == "0")) {
                    var newbillerid = parent.window.getSystemAppID();
                    mini.getbyName("FBillerID").setValue(newbillerid);
                }

                var MainData = form.getData();
                if (MainData.FStockID == undefined) {
                    mini.alert("要货门店未选!");
                    return false;
                }
                if (MainData.FRStockID == undefined) {
                    mini.alert("配送门店未选!");
                    return false;
                }
                if (MainData.FRStockID == MainData.FStockID) {
                    mini.alert("要货门店与配送门店不能相同");
                    return false;
                }
                return true;
            }

            function saveBill() {
                BillState(1);//先锁定
                if (checkValid() == false) {
                    BillState(4);//先锁定
                    return;
                }
                var MainData = form.getData();
                var cDate = mini.getbyName("FDate").getFormValue();
                var rCnt = grid.getData().length;
                var sBillNo = MainData.FBillNo;

                var mjson = "[{'KInterID':'" + MainData.KInterID + "',";
                mjson = mjson + "'FBillNo':'" + MainData.FBillNo + "',";
                mjson = mjson + "'FSystemAppID':'" + uID + "',";
                mjson = mjson + "'FRStockID':'" + MainData.FStockID + "',";
                mjson = mjson + "'FStockID':'" + MainData.FRStockID + "',"; 
                mjson = mjson + "'FPlatformID':'0',";
                mjson = mjson + "'FDate':'" + cDate + "',";
                mjson = mjson + "'FDataType':'" + datatype + "',";
                mjson = mjson + "'FRemark':'" + MainData.FRemark + "',";
                var json = "'goods_list':[";
                for (var i = 0; i < rCnt; i++) {
                    var rs = grid.getRow(i);
                    if (typeof (rs.mFNumber) == "undefined") continue;
                    js = "{"
                    js = js + '"mFItemID":' + rs.mFItemID + ",";
                    var qty = typeof (rs.mFQty) == "undefined" ? 0 : rs.mFQty;
                    js = js + '"mFQty":' + qty + ",";
                    var price = typeof (rs.mFPrice) == "undefined" ? 0 : rs.mFPrice;
                    js = js + '"mFPrice":' + price + ",";
                    var money = typeof (rs.mFAmount) == "undefined" ? 0 : rs.mFAmount;
                    js = js + '"mFAmount":' + money + ",";
                    var remark = typeof (rs.mFRemark) == "undefined" ? "" : rs.mFRemark;
                    js = js + '"mFRemark":"' + remark + '"';
                    js = js + "}"
                    json = json + js + ","

                }
                json = json.substring(0, json.length - 1);
                json = json + "]";
                if (json.length < 8) {
                    mini.alert("没有要保存的数据！");
                    return;
                }
                mjson = mjson + json + "}]";
                $.ajax({
                    url: '../Delivery/SaveBillData_RGoods',
                    data: { mdatajson: mjson },
                    type: "post",
                    cache: false,
                    success: function (txt) {
                        var ret = mini.decode(txt);
                        if (ret.id != -1) {
                            mini.showTips({ content: '保存成功', state: "success", x: "center", y: "top", timeout: 3000 });
                            mini.getbyName("KInterID").setValue(ret.id);
                            mini.getbyName("FBillNo").setValue(ret.FBillNo);
                            BillState(1);
                            mini.getbyName("ScanAdd").setEnabled(false);
                        }
                        else {
                            mini.alert("异常:" + ret.Status);
                        }
                    },
                    error: function (xhr) {
                        mini.alert("异常:" + xhr.responseText);
                    }
                });
            }

            function onButtonEditImg(e) {
                ShowGoodsWinImg("");
            }

            function ShowGoodsImg(txtValue) {
                var FObjectID = mini.getbyName("FRStockID").getValue();
                var FStockID = mini.getbyName("FStockID").getValue();
                var json = "[{";
                json = json + "'FClassID':" + "'0',";
                json = json + "'FObjectID':" + "'" + FObjectID + "',";
                json = json + "'FStockID':" + "'" + FStockID + "',";
                json = json + "'FBillType':" + "'" + datatype + "',";
                json = json + "'FSupplierID':" + "'-1',";
                json = json + "'FNumber':'" + txtValue + "',";
                json = json + "'FSystemAppID':'" + uID + "'}]";
                if ((txtValue != null) && (txtValue != "")) {
                    $.post("../Bill/GetRequireGood", { datajson: json }, function (data) {
                        var ret = mini.decode(data);
                        if (ret.data.length == 1) {
                            var row = grid.getSelected();
                            var action = ret.data[0];
                            if (action.FRequireStop != 1) {
                                var newaction = Extra({
                                    mFItemID: action.FItemID, mFNumber: action.FNumber, mFName: action.FName, mFModel: action.FSpec,
                                    mFUnitName: action.FUnitName, mFQty: row == null ? 0 : row.mFQty, mFPrice: action.FPrice, mFCode: action.FCode,
                                    mFStockQty: action.FStockQty, mFPosPrice: action.FPosPrice, mFPurPrice: action.FPurPrice
                                });
                                grid.updateRow(row, newaction);
                                if ((row.mFQty != "NaN") && (row.mFQty != 0)) {
                                    grid.updateRow(row, {
                                        mFAmount: row.mFQty * action.FPrice,
                                        mFPurAmount: row.mFQty * action.FPurPrice,
                                        mFPosAmount: row.mFQty * action.FPosPrice
                                    });
                                }
                            } else {
                                ShowGoodsWinImg(txtValue);
                            }
                        }
                        else {
                            ShowGoodsWinImg(txtValue);
                        }
                    });
                }
                else {
                    ShowGoodsWinImg(txtValue);
                }
            }

            function ShowGoodsWinImg(txtValue) {
                var FObjectID = mini.getbyName("FRStockID").getValue();
                var FStockID = mini.getbyName("FStockID").getValue();
                var w = window.innerWidth || document.body.clientWidth;
                var h = window.innerHeight || document.body.clientHeight;
                w = w + 24 + 150, h = h + 46 + 37 + 20;
                if (w > 1380) w = 1380;
                if (h > 700) h = 700;
                mini.open({
                    url: "../Bill/GetRequireGoods",
                    title: "选择商品", width: w, height: h,
                    onload: function () {
                        var iframe = this.getIFrameEl();
                        var data = { key: txtValue, mBillType: datatype, mStockID: FStockID, FObjectID: FObjectID };
                        iframe.contentWindow.SetData(data);
                    },
                    ondestroy: function (action) {
                        if (action) {
                            var row = grid.getSelected();
                            for (var i = 0, l = action.length; i < l; i++) {
                                var rw = action[i];
                                if (typeof (row.mFItemID) != "undefined") {
                                    var ix = grid.indexOf(row);
                                    ix++;
                                    if (ix >= grid.getData().length) {
                                        var newRow = {
                                            mFItemID: rw.FItemID, mFNumber: rw.FNumber, mFName: rw.FName, mFModel: rw.FSpec,
                                            mFUnitName: rw.FUnitName, mFPrice: rw.FPrice, mFCode: rw.FCode,
                                            mFStockQty: rw.FStockQty, mFPosPrice: rw.FPosPrice, mFPurPrice: rw.FPurPrice, mFQty: rw.mFQty,
                                            mFAmount: rw.mFQty * rw.FPrice
                                        };
                                        grid.addRow(Extra(newRow), grid.getData().length);
                                        row = grid.getRow(ix);
                                    }
                                    else {
                                        row = grid.getRow(ix);
                                        grid.cancelEdit();
                                        grid.updateRow(row, Extra({
                                            mFItemID: rw.FItemID, mFNumber: rw.FNumber, mFName: rw.FName, mFModel: rw.FSpec,
                                            mFUnitName: rw.FUnitName, mFPrice: rw.FPrice, mFCode: rw.FCode,
                                            mFStockQty: rw.FStockQty, mFPosPrice: rw.FPosPrice, mFPurPrice: rw.FPurPrice, mFQty: rw.mFQty,
                                            mFAmount: rw.mFQty * rw.FPrice
                                        }));
                                    }
                                }
                                else {
                                    grid.cancelEdit();
                                    var arow = {
                                        mFItemID: rw.FItemID, mFNumber: rw.FNumber, mFName: rw.FName, mFModel: rw.FSpec,
                                        mFUnitName: rw.FUnitName, mFPrice: rw.FPrice, mFCode: rw.FCode,
                                        mFStockQty: rw.FStockQty, mFPosPrice: rw.FPosPrice, mFPurPrice: rw.FPurPrice, mFQty: rw.mFQty,
                                        mFAmount: rw.mFQty * rw.FPrice
                                    };
                                    var newRow = Extra(arow);
                                    grid.updateRow(row, newRow);
                                }
                            }
                            if (action.length > 0) mini.get("save").setEnabled(true);//编辑后保存按钮可用
                        }
                    }
                });
            }

            function ExcelImport() {
                var FSystemAppID = parent.window.getSystemAppID();
                mini.open({
                    url: "../Bill/ImportData?mDataType=10&mSystemAppID=" + FSystemAppID + "&FStockID=" + mini.getbyName("FStockID").getValue(),
                    title: "Excel导入", width: 450, height: 200,
                    ondestroy: function (action) {
                        if (action == "close")
                            return;
                        if (action) {
                            if (action.length > 0) {
                                if (grid.getData().length > 0) {
                                    if (grid.getData()[grid.getData().length - 1].mFNumber == undefined) {
                                        var row = grid.getRow(grid.getData().length - 1);
                                        grid.removeRow(row, false);
                                    }
                                }
                                for (var i = 0, l = action.length; i < l; i++) {
                                    var rw = action[i];
                                    if (rw.FRequireStop != 1) {
                                        var newRow = {
                                            mFItemID: rw.FItemID, mFNumber: rw.FNumber, mFName: rw.FName, mFModel: rw.FSpec,
                                            mFUnitName: rw.FUnitName, mFPrice: rw.FPrice, mFCode: rw.FCode, mFStockQty: rw.FStockQty,
                                            mFPosPrice: rw.FPosPrice, mFPurPrice: rw.FPurPrice, mFQty: rw.FQty, mFAmount: rw.FQty * rw.FPrice
                                        };
                                        grid.addRow(newRow, grid.getData().length);
                                    }
                                }
                                BillState(4);
                                mini.get("ScanAdd").setEnabled(true);
                            }
                        }
                    }
                });
            }

            function onValueChangedImg(e) {
                ShowGoodsImg(e.value);
            }            
</script>
